package com.cxria.script

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook

class SuperExecl{

    List<Class> cellValueClazzs

    Workbook createWorkbook(String xlsName){
        def workbook
        if(xlsName.endsWith('.xls')){
            workbook = new HSSFWorkbook()
        } else if(xlsName.endsWith('.xlsx')){
            workbook = new XSSFWorkbook()
        }
        workbook
    }

    Workbook getWorkbook(String xlsName){
        def workbook
        def fis = new FileInputStream(xlsName)
        if(xlsName.endsWith('.xls')){
            workbook = new HSSFWorkbook(fis)
        } else if(xlsName.endsWith('.xlsx')){
            workbook = new XSSFWorkbook(fis)
        }
        workbook
    }

    Sheet createSheet(Workbook workbook, Map titleInfo){
        def sheet = workbook.createSheet()
        def rowIndex = 0
        def titleRow = sheet.createRow(0)
        titleInfo.each { titleName, titleWidth ->
            sheet.setColumnWidth(rowIndex, titleWidth)
            def cell = titleRow.createCell(rowIndex)
            cell.setCellValue(titleName)
            rowIndex ++
        }
        sheet
    }

    void writeLine(Sheet sheet, Integer lineIndex, List data){
        def row = sheet.createRow(lineIndex)
        def cellIndex = 0
        data.each { cellValue ->
            if(cellValue){
                def cell = row.createCell(cellIndex)
                cell.setCellValue(cellValue)
            }
            cellIndex ++
        }
    }

    void writeToFile(String xlsName, Workbook workbook){
        def outputStream = new FileOutputStream(xlsName)
        workbook.write(outputStream)
    }

    Sheet getSheet(Workbook workbook, int sheetIndex, List<Class> cellValueClazzs){
        setCellValueClazzs(cellValueClazzs)
        workbook.getSheetAt(sheetIndex)
    }

    List getData(Sheet sheet, int startLine){
        def infos = []
        def rowNum = sheet.lastRowNum
        (startLine..rowNum).each { rowIndex ->
            def row = sheet.getRow(rowIndex)
            def rowData = []
//            print "${rowIndex}: "
            (0..cellValueClazzs.size() - 1).each { cellIndex ->
                def cell = row.getCell(cellIndex)
                    def cellClazz = cellValueClazzs[cellIndex]
                    def cellValue
                if(cell){
                    if(String.class == cellClazz){
                        cellValue = cell.stringCellValue
                    } else if(Integer.class == cellClazz){
                        cellValue = cell.numericCellValue
                    }
                }
                if(cellValue == null){
                    cellValue = ''
                }
//                println "${cellIndex}, ${cellValue}"
                    rowData.add(cellIndex, cellValue)
//                    print "${cellValue}\t"
            }
            infos.add(rowIndex - startLine, rowData)
//            println ''
        }
        infos
    }

}
